<?php
require('../database/db.php');
require('../../tool/UpFileTool.class.php');
$mysql=new mysql();
$ordersid=$_REQUEST['ordersid'];
if ($ordersid) {
	$sql = "select state from goodsorders where ordersid=".$ordersid;
	$state = $mysql->getOne($sql);
	if (in_array($state,array(1,2,3,7))) {
		// 调用信息推送接口
		$url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=4&ordersid=".$ordersid;
		file_get_contents($url);
		// 调用信息推送接口 end
		$sql = "UPDATE goodsorders SET state=4 where ordersid=".$ordersid;
		$mysql->query($sql);
		if ($_FILES) {
			$UpFileTool=new UpFileTool('user_images');
			$UpFileTool->multiImage($w=500, $h=500 ,$sumbimage='../../sumbimage', $srcimage='../../srcimage');
			$user_images = $UpFileTool->multiSumbImage;
			$user_srcimages = $UpFileTool->multiSrcImage;
		}
		$sql = "INSERT INTO tuihuo (ordersid,user_comment,user_images,user_srcimages) VALUES ('".$ordersid."','".$_REQUEST[user_comment]."','".$user_images."','".$user_srcimages."')";
		$mysql->query($sql);
		$id = $mysql->insert_id();

		// 查找商家确认已收回货时间
		$query = mysql_query("select return_confirm_time from categroy_type where id=(select categroy_type_id from categroy where cateid=(select leimu from shop where shopid=(select shopid from goodsorders where ordersid=".$ordersid.")))");
		$categroy_type = mysql_fetch_assoc($query);
		$return_confirm_time = $categroy_type[return_confirm_time];
		// 查找商家确认已收回货时间 end
		// 定时器
		$url = "http://120.24.0.10/xzpf/changeAfterTime/changeTuihuoState.php?ordersid=".$ordersid."";
		$start_date = date("Y-m-d",strtotime(''.$return_confirm_time.'days'));
		$start_hour = date("H");
		$start_minute = date("i");
		$tag = 'setTuihuoState';
		$loop = 1;

		$apiUrl = "http://120.24.0.10/unitask/core/func.add.php?url=".$url."&start_date=".$start_date."&start_hour=".$start_hour."&start_minute=".$start_minute."&tag=".$tag."&loop=".$loop."";
		file_get_contents($apiUrl);
		// 定时器 end

		if ($id>0) {
			
			$arr = array('code' => 100, 'id' => $id, 'tip' =>'退货申请成功,等待审核,id=>退货id');
		}else{
			$arr = array('code' => 300, 'tip' =>'退货申请失败');
		}
	}else{
		$arr = array('code' => 400, 'tip' =>'该订单状态不许申请退货');
	}
	
}else{
	$arr = array('code' => 200, 'tip' =>'参数传入有误,必传入参数:订单id=>ordersid;可选参数:用户退货说明=>user_comment,用户退货说明附图=>user_images');
}
echo json_encode($arr);
?>